package com.thornbird.vehiclerecycling.util;

import com.thornbird.vehiclerecycling.entity.BaseEntity;

import javax.persistence.Column;
import java.lang.reflect.Field;

public class DataUtil {

    public static void fillDefaultValue(BaseEntity entity) throws IllegalAccessException {
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            String typeName = field.getType().getName();
            Column column = field.getAnnotation(Column.class);
            if (column != null && !column.nullable()) {
                field.setAccessible(true);
                if (typeName.equals("java.lang.Integer")) {
                    if (field.get(entity) == null) {
                        field.set(entity, 0);
                    }
                } else if (typeName.equals("java.lang.String")) {
                    if (field.get(entity) == null) {
                        field.set(entity, "");
                    }
                } else if (typeName.equals("java.util.Date")) {
                    if (field.get(entity) == null) {
                        field.set(entity, DateUtil.getData(2000, 0, 1));
                    }
                }
            }
        }
    }

}
